import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:navigator_demo/navigator1pointzero/bottom_bar_menu_guest_page.dart';
import 'package:navigator_demo/navigator1pointzero/bottom_bar_menu_page.dart';
import 'package:navigator_demo/navigator1pointzero/common_page.dart';
import 'package:navigator_demo/navigator_warpper/page_navigator.dart';

void main() {
  runApp(App());
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'debug-navigator',
      home: HomePage(),
      onGenerateRoute: (settings) {
        return MaterialPageRoute(builder: (context) {
          return CommonPage(settings: settings);
        });
      },
    );
  }

  @override
  Future<bool> didPopRoute() {
    return super.didPopRoute();
  }
}

class TabANavigator extends StatelessWidget with RebuildGuestModePages {
  final _navigatorKey = GlobalKey<NavigatorState>();
  @override
  Widget build(BuildContext context) {
    print('TabANavigator build');
    if (_navigatorKey.currentState != null) {}
    return PageNavigator(
      mainTab: 'TabANavigator',
      pageRoutes: onNaviAGenerateRoute,
      // key: _pageKey,
      navigatorKey: _navigatorKey,
    );
  }
}

class TabBNavigator extends StatelessWidget with RebuildGuestModePages {
  final _navigatorKey = GlobalKey<NavigatorState>();
  @override
  Widget build(BuildContext context) {
    print('TabBNavigator build');
    return PageNavigator(
      mainTab: 'TabBNavigator',
      pageRoutes: onNaviBGenerateRoute,
      navigatorKey: _navigatorKey,
    );
  }
}

// ignore: must_be_immutable
class HomePage extends StatelessWidget {
  final ValueNotifier<int> bottomMenuPageNotifier = ValueNotifier(0);
  final ValueNotifier<int> tabIndexNotifier = ValueNotifier(0);
  Widget _tabA;
  Widget _tabB;
  ValueSetter<NavigatorState> navigate;

  HomePage() {
    _tabA = TabANavigator();
    _tabB = TabBNavigator();
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
      valueListenable: bottomMenuPageNotifier,
      builder: (context, bottomMenuPageIndex, child) {
        if (bottomMenuPageIndex == 0) {
          return BottmBarMenuPage(
            tabIndexNotifier: tabIndexNotifier,
            tabA: (_tabA as RebuildGuestModePages)..rebuildChildIfNeeded(),
            tabB: (_tabB as RebuildGuestModePages)..rebuildChildIfNeeded(),
          );
        } else {
          return BottmBarMenuGuestPage(
            tabIndexNotifier: tabIndexNotifier,
            tabA: (_tabA as RebuildGuestModePages)..rebuildChildIfNeeded(),
            tabB: (_tabB as RebuildGuestModePages)..rebuildChildIfNeeded(),
          );
        }
      },
    );
  }
}

mixin RebuildGuestModePages on Widget {
  final _refreshPages = [
    'CommonPage',
    'TabA',
    'TabB',
  ];

  void rebuildChildIfNeeded() {
    SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
      vistiorChildElements(_navigatorKey.currentContext);
    });
  }

  GlobalKey<NavigatorState> get _navigatorKey;

  void vistiorChildElements(Element element) {
    if (element == null) return;
    element.visitChildElements((element) {
      if (_refreshPages.contains(element.widget.runtimeType.toString())) {
        element.markNeedsBuild();
      }
      vistiorChildElements(element);
    });
  }
}
